package com.rjwh.dingdong.client.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.rjwh.dingdong.client.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 社区发帖--图片列表适配器
 *
 * @author 王小珂
 * @ClassName CommunityPublishTopicPicAdapter
 * @Description TODO(这里用一句话描述这个类的作用)
 * @date 2016-4-25 上午11:59:36
 * @history 1.YYYY-MM-DD author: description:
 */
public class CommunityPublishTopicPicAdapter extends
        RecyclerView.Adapter<CommunityPublishTopicPicAdapter.ViewHolder> {
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 1;
    private LayoutInflater mInflater;
    private View mHeaderView;
    private List<String> picList = new ArrayList<String>();
    private Context context;

    public CommunityPublishTopicPicAdapter(Context context) {
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
    }

    public void clear() {
        picList.clear();
        notifyDataSetChanged();
    }

    public void addDatas(List<String> datas) {
        picList.addAll(datas);
        notifyDataSetChanged();
    }

    private OnItemClickListener mListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null)
            return TYPE_NORMAL;
        if (position == 0)
            return TYPE_HEADER;
        return TYPE_NORMAL;
    }

    @Override
    public int getItemCount() {
        return mHeaderView == null ? (picList == null ? 0 : picList.size())
                : picList.size() + 1;
    }

    public List<String> getPicList() {
        return picList;
    }

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }

    public View getHeaderView() {
        return mHeaderView;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        if (getItemViewType(position) == TYPE_HEADER)
            return;

        final int pos = getRealPosition(viewHolder);
        final String data = picList.get(pos);
        if (viewHolder != null) {
            Glide.with(context).load(picList.get(pos)).dontAnimate().placeholder(R.drawable.icon_pic_holder).error(R.drawable.icon_pic_holder).diskCacheStrategy(DiskCacheStrategy.ALL).into(viewHolder.ivPhoto);
//			BitmaoLocalLoadTask.getInstance().doDisplay(viewHolder.ivPhoto,
//					picList.get(pos));
            if (mListener == null)
                return;
            viewHolder.itemView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(pos, data);
                }
            });
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        if (mHeaderView != null && viewType == TYPE_HEADER)
            return new ViewHolder(mHeaderView);
        View view = mInflater.inflate(R.layout.mood_send_grid_pic_item,
                viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.ivPhoto = (ImageView) view
                .findViewById(R.id.mood_send_pic_item_image);
        return viewHolder;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View arg0) {
            super(arg0);
        }

        ImageView ivPhoto;
    }

    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getPosition();
        return mHeaderView == null ? position : position - 1;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, String data);
    }
}
